﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WK.Libraries.SharpClipboardNS;
using static WK.Libraries.SharpClipboardNS.SharpClipboard;

namespace Tool.Helper
{
    public class ClipboardHelper:IDisposable
    {
        private SharpClipboard _sClip;
        public ClipboardHelper()
        {
            _sClip = new SharpClipboard();
        }
        public   Image GetImage(string type)
        {
            var dataObject = Clipboard.GetDataObject();
            if (dataObject != null)
            {
                if (dataObject.GetDataPresent(type))
                {
                    var bitmap = dataObject.GetData(type) as Bitmap;
                    return bitmap;
                }
            }
            return null;
        }
        public  Image GetImage()
        {
            if (Clipboard.ContainsImage())
            {
                return Clipboard.GetImage();
            }
            return null;
        }

        public static string GetText()
        {
            return Clipboard.GetText();
        }
        public static void SetText(string str)
        {
            Clipboard.SetText(str);
        }

        private System.EventHandler<ClipboardChangedEventArgs> _handler;
        public  void CheckChange(System.EventHandler<ClipboardChangedEventArgs > test)
        {
            _sClip.ClipboardChanged+= test;
            _handler = test;
        }

        public void Dispose()
        {
            if(_handler != null)
            {
                _sClip.ClipboardChanged -= _handler;
            }
            _sClip?.Dispose();
        }
    }
}
